<template>
  <div>
    <div class="serchWrapper">
      <input
        type="text"
        placeholder="请输入关键字(歌名，歌手，专辑)"
        class="searchInput"
        v-model="searchKeywords"
        @input="getSearchResults"
      />
      <button @click="searchKeywords=''" class="clearKeywords" v-if="searchKeywords">取消</button>
    </div>
    <div class="recommend">
      <div class="defaultListWrapper" v-if="defaultSongs.length&&!searchKeywords">
        <mt-button
          type="primary"
          size="small"
          class="checkButton"
          v-for="(item, index) in defaultSongs"
          :key="index"
          @click="openSongsByid(item.id)"
        >{{item.name}}</mt-button>
      </div>
    </div>
    <div class="searchResultLists" v-if="searchKeywords">
      <ul>
        <li
          v-for="(item, index) in searchResultLists"
          :key="index"
          @click="openSongsByid(item.id)"
        >
          <p>
            <span>
              <svg class="icon" aria-hidden="true">
                <use xlink:href="#icon-duolaAmeng" />
              </svg>
            </span>
            <span class="name">{{item.name}}</span>
            <span>作者：{{item.artists[0].name}}</span>
            <span>专辑：{{item.album.name}}</span>
          </p>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { getSearchSongLists, getSearchHotSongs } from "../../api/recommend.js";
export default {
  data() {
    return {
      defaultSongs: [],
      searchKeywords: "",
      searchResultLists: [],
       wrapperHeight: 0,
    };
  },
  created() {
    getSearchHotSongs().then(res => {
      for (let i = 0; i < 10; i++) {
        let n = Math.floor(Math.random() * 100);
        this.defaultSongs.push(res.data.data[n]);
      }
    });
    
  },
  methods: {
       openSongsByid(id){
          this.$store.dispatch("checkedSongs",id)
      },
    getSearchResults() {
      if (this.searchKeywords) {
        getSearchSongLists(this.searchKeywords)
          .then(res => {
            this.searchResultLists = res.data.result.songs;
          })
          .catch(err => {
              throw err
          });
      }
    }
  }
};
</script>

<style scoped lang="stylus">
.page-loadmore-wrapper {
  overflow: scroll; 
  -webkit-overflow-scrolling : touch; 
  &::-webkit-scrollbar{
    display: none;
}
}
.icon {
  width: 30px;
  height: 30px;
  vertical-align: -10px;
  fill: currentColor;
  overflow: hidden;
}
.searchResultLists
    width 100%
    position absolute
    height: 100%;
    overflow: scroll;
    // top 350px
    ul
        li
            height 40px
            width 100%
            padding 10px 0px 
            border-bottom 1px dashed #ffffff
            background rgba(233,233,216,0.2)
            p
                font-size 18px
                background rgba(233,233,216,0.4)
                line-height 40px 
                display inline-block
                width 100%
                text-align left 
                text-indent 20px
                overflow:hidden
                white-space: nowrap
                text-overflow: ellipsis
                .name
                    font-weight 700
                    font-size 20px
                    color gold
.serchWrapper
    display flex
    justify-content space-around
.clearKeywords
    font-size 20px
    line-height 50px
    height 30px
    background transparent
    color #ffffff
    border 0
    outline none
.searchInput
    height 30px
    width 80%
    border-radius 15px
    margin 10px 0px
    text-indent 10px
    font-size 14px
    outline none 
    border 0
.checkButton
    border-radius 20px
    margin-top 25px
    display block
    float left
    margin 10px
    border 1px solid #fff
</style>